package com.zenithmind.chat.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

/**
 * 消息状态枚举 - 遵循开闭原则
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Getter
public enum MessageStatus {
    
    /**
     * 正常
     */
    NORMAL(1, "正常"),
    
    /**
     * 已撤回
     */
    RECALLED(2, "已撤回"),
    
    /**
     * 已删除
     */
    DELETED(3, "已删除"),
    
    /**
     * 发送失败
     */
    FAILED(4, "发送失败"),
    
    /**
     * 发送中
     */
    SENDING(5, "发送中");

    /**
     * 状态码 - 用于数据库存储
     */
    @EnumValue
    @JsonValue
    private final Integer code;

    /**
     * 状态描述
     */
    private final String description;

    /**
     * 构造函数
     */
    MessageStatus(Integer code, String description) {
        this.code = code;
        this.description = description;
    }

    /**
     * 根据状态码获取枚举
     * 
     * @param code 状态码
     * @return 对应的枚举值
     */
    public static MessageStatus fromCode(Integer code) {
        if (code == null) {
            return NORMAL;
        }
        
        for (MessageStatus status : values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        
        throw new IllegalArgumentException("未知的消息状态码: " + code);
    }

    /**
     * 检查是否为正常状态
     * 
     * @return 是否正常
     */
    public boolean isNormal() {
        return this == NORMAL;
    }

    /**
     * 检查是否已撤回
     * 
     * @return 是否已撤回
     */
    public boolean isRecalled() {
        return this == RECALLED;
    }

    /**
     * 检查是否已删除
     * 
     * @return 是否已删除
     */
    public boolean isDeleted() {
        return this == DELETED;
    }

    /**
     * 检查是否发送失败
     * 
     * @return 是否发送失败
     */
    public boolean isFailed() {
        return this == FAILED;
    }

    /**
     * 检查是否正在发送
     * 
     * @return 是否正在发送
     */
    public boolean isSending() {
        return this == SENDING;
    }

    /**
     * 检查消息是否可见
     * 
     * @return 是否可见
     */
    public boolean isVisible() {
        return this == NORMAL || this == RECALLED;
    }
}
